package com.guguju.ssms.core.base;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.Window;

import com.google.gson.JsonObject;
import com.guguju.ssms.core.http.HttpLoader;

//import butterknife.ButterKnife;

/**
 * Created by dell on 2022-5-14.
 */
public class BaseActivity extends AppCompatActivity {


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //ButterKnife.bind(this);
        registerReceiver(mHomeKeyEventReceiver, new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mHomeKeyEventReceiver);
    }

    @Override
    protected void onResume() {
        super.onResume();

        if (toBack) {
            toBack = false;
            reBack();
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = this.getWindow();
            //状态栏改变颜色。
            window.setStatusBarColor(Color.BLACK);
        }
    }

    @Override
    protected void onPause() {
        super.onPause();

    }

    /**
     * 监听是否点击了home键将客户端推到后台
     */
    private BroadcastReceiver mHomeKeyEventReceiver = new BroadcastReceiver() {
        String SYSTEM_REASON = "reason";
        String SYSTEM_HOME_KEY = "homekey";

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
                String reason = intent.getStringExtra(SYSTEM_REASON);
                if (TextUtils.equals(reason, SYSTEM_HOME_KEY)) {
                    //表示按了home键,程序到了后台
//                    ToastUtils.showShortToast(getApplicationContext(), "home");
                    toBack = true;
                    enterBack();
                }
            }
        }
    };

    /**
     * 压入后台
     */
    private boolean toBack = false;


    /**
     * 压入后台
     */
    private void enterBack() {
      /*   JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("macId", DeviceUtils.getMacAddress(this));

       HttpLoader.post(UrlConstant.ENTER_BACKGROUND, jsonObject, new JsonCallback<BaseResponse<ToBack>>() {
            @Override
            public void onSuccess(BaseResponse<ToBack> baseResponse, Call call, Response response) {
                //表示按了home键,程序到了后台
                LogUtil.i("压入后台");
                if (baseResponse.code == 200) {
                    ToBack data = baseResponse.data;
                    String interfacetoken = data.getInterfacetoken();
                    if (TextUtils.isEmpty(interfacetoken)) {
                        QiJu.interfaceToken = interfacetoken;
                        SPUtils schemeSp = new SPUtils(BaseActivity.this, SPConstant.SP_XML_NAME);
                        schemeSp.putString(SPConstant.interfaceToken, interfacetoken);
                    }
                }
            }
        });*/
    }

    /**
     * 进入前台
     */
    private void reBack() {
        /*  JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("macId", DeviceUtils.getMacAddress(this));

      HttpLoader.post(UrlConstant.RE_BACK_MAIN, jsonObject, new JsonCallback<BaseResponse<ToBack>>() {
            @Override
            public void onSuccess(BaseResponse<ToBack> baseResponse, Call call, Response response) {
                LogUtil.i("进入前台");
                if (baseResponse.code == 200) {
                    ToBack data = baseResponse.data;
                    String interfacetoken = data.getInterfacetoken();
                    if (TextUtils.isEmpty(interfacetoken)) {
                        QiJu.interfaceToken = interfacetoken;
                        SPUtils schemeSp = new SPUtils(BaseActivity.this, SPConstant.SP_XML_NAME);
                        schemeSp.putString(SPConstant.interfaceToken, interfacetoken);
                    }
                }
            }
        });*/
    }
}
